package com.chilly.netty.websocket.action.stringTransaction;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * Created by Chilly Cui on 2020/12/25.
 */
public class HandlerServerHello extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //处理收到的消息，并反馈消息到客户端
        ByteBuf in = (ByteBuf) msg;
        System.out.println("收到客户端消息："+in.toString(CharsetUtil.UTF_8));

        //写入并发送消息到客户端
        ctx.writeAndFlush(Unpooled.copiedBuffer("你好，我是服务端，已经收到发送的消息",CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
